package priv.happy.mealpickpro.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import priv.happy.mealpickpro.domain.Address;
import priv.happy.mealpickpro.emum.SystemStatusCode;
import priv.happy.mealpickpro.service.AddressService;
import priv.happy.mealpickpro.vo.AddressVO;
import priv.happy.mealpickpro.vo.ApiResponse;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: mealPickPro
 * @Package_name: priv.happy.mealpickpro.controller
 * @Date: 2023/10/21 9:58
 * @Description:
 */
@RestController
@RequestMapping("/api/v2/address")
public class AddressController {
    @Autowired
    AddressService addressService;


    /**
     * 查询用户的所有地址信息
     *
     * @param userId 用户的id
     * @return 用户所有的地址信息
     */
    @GetMapping("/list/{userId}")
    public ApiResponse<List<AddressVO>> list(@PathVariable Integer userId) {
        ApiResponse<List<AddressVO>> apiResponse = new ApiResponse<>();
        if (userId != null && userId > 0) {
            apiResponse.setData(addressService.getAddressListByUserId(userId))
                    .setCode(SystemStatusCode.SUCCESS.getCode())
                    .setSuccess(true).setMessage("查询用户地址信息成功");
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("查询用户地址信息失败");
        }

        return apiResponse;
    }

    /**
     * 查询当前用户的默认地址
     *
     * @param userId 用户id
     * @return 返回当前用户的默认地址信息
     */
    @GetMapping("/default/{userId}")
    public ApiResponse<AddressVO> getById(@PathVariable Integer userId) {
        ApiResponse<AddressVO> apiResponse = new ApiResponse<>();
        if (userId != null && userId > 0) {
            apiResponse.setData(addressService.getDefaultByUserId(userId))
                    .setCode(SystemStatusCode.SUCCESS.getCode()).setSuccess(true).setMessage("查询用户默认地址信息成功");
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("查询用户默认地址信息失败");
        }

        return apiResponse;
    }

    /**
     * 添加一条地址信息
     *
     * @param address 要添加的地址信息
     * @return
     */
    @PostMapping
    public ApiResponse<String> addAddress(@RequestBody Address address) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (address.getUserId() != null && address.getUserRole() != null && address.getAddress() != null) {
            if (addressService.isFirst(address.getUserId())) {
                addressService.save(address.setStatus(1));
            } else {
                addressService.save(address);
            }
            apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).
                    setSuccess(true).setMessage("添加地址信息成功");
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("添加地址信息失败");
        }

        return apiResponse;
    }


    /**
     * 修改已有地址信息
     *
     * @param address 要修改的地址信息
     * @return 返回修改成功与否的结果
     */
    @PutMapping
    public ApiResponse<String> updateAddress(@RequestBody Address address) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (address.getId() != null && address.getUserId() != null && address.getAddress() != null) {
            if (addressService.updateAddressById(address)) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).
                        setSuccess(true).setMessage("修改用户地址信息成功");
            } else {
                // 可能修改的是他人的地址信息，或者出现了异常
                apiResponse.setCode(SystemStatusCode.UNAUTHORIZED.getCode()).
                        setSuccess(false).setMessage("修改用户地址信息失败");
            }
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("修改用户地址信息失败");
        }

        return apiResponse;
    }

    /**
     * 修改默认地址
     *
     * @param address 要修改为哪个默认地址，只有id和userId是有值的
     * @return 返回修改成功与否的结果
     */
    @PutMapping("/default")
    public ApiResponse<String> updateDefaultAddress(@RequestBody Address address) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (address.getId() != null && address.getUserId() != null) {
            if (addressService.updateDefaultAddressById(address)) {
                apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).
                        setSuccess(true).setMessage("修改用户默认地址信息成功");
            } else {
                // 可能修改的是他人的地址信息，或者出现了异常
                apiResponse.setCode(SystemStatusCode.UNAUTHORIZED.getCode()).
                        setSuccess(false).setMessage("修改用户默认地址信息失败");
            }
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("修改用户地址信息失败");
        }

        return apiResponse;
    }


    /**
     * 根据地址id删除用户的一条地址信息
     *
     * @param userId 用户id
     * @param id     地址信息
     * @return 返回删除的结果
     */
    @DeleteMapping
    public ApiResponse<String> deleteAddress(Integer userId, Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (userId != null && userId > 0 && id != null && id > 0) {
            addressService.removeUserAddress(userId, id);
            apiResponse.setCode(SystemStatusCode.SUCCESS.getCode()).setSuccess(true).setMessage("删除地址信息成功");
        } else {
            apiResponse.setCode(SystemStatusCode.BAD_REQUEST.getCode()).
                    setSuccess(false).setMessage("删除地址信息失败");
        }

        return apiResponse;
    }
}
